import { useState } from "react"

import { PageType } from "../business/types"
import http from "../helper/request"
import {
	BalanceInfo,
	IAgentAndLossRakingDataList,
	IAgentAndLossRankingQueryParams,
	IAgentProfitDayTotalList,
	IAgentProfitLossDetailList,
	IAgentProfitLossList,
	IAgentProfitLossState,
	IBetDetailList,
	IBottomQuery,
	IDailyDetailData,
	IDailyDetailDxReportDParams,
	IDailyDxReportD,
	IDailyDxReportDParams,
	IDataCompareDay,
	IDataCompareDayParams,
	IGameList,
	IGameProfitList,
	IGameProfitSummaryState,
	IGetMemberNetAmountDetailParams,
	IGetMemberNetAmountDetailResponse,
	IMemberExpectedList,
	IMemberLossRankingList,
	IMemberLossRankingTotal,
	IMemberRateDetailList,
	IMemberReportList,
	IMemberReportQueryParams,
	IMemberReturnDataList,
	IMemberReturnDataQueryParams,
	IMemberReturnDataReportList,
	IMemberReturnDataReportParams,
	IMemberReturnDataSummary,
	IMemberTeamRebate,
	IName,
	IPlayerDailyStatsList,
	IProxyNetAmountOrderList,
	IProxyNetAmountQuery,
	IProxyNetAmountRecalList,
	IProxyProfitAndLoss,
	IProxyProfitAndLossListParams,
	IProxyReportList,
	IQueryAgentProfitLoss,
	IQueryAgentProfitLossDetail,
	IQueryBetDetail,
	IQueryGameProfitDetail,
	IQueryGameProfitQuery,
	IQueryMemberExpected,
	IQueryMemberLossRanking,
	IQueryMemberRateDetail,
	IQueryMemberReport,
	IQueryMemberTeamRebate,
	IQueryProxyNetAmountOrder,
	IQueryProxyReportList,
	IQueryRebateReconciliation,
	IQueryStatusOverviewTripartite,
	IQueryVenueProfitDetail,
	IQueryVenueProfitLoss,
	IRebateReconciliationList,
	IRebateVenueState,
	IStatusBottomTripartiteList,
	IStatusOverviewTripartiteList,
	IStatusOverviewTripartiteState,
	ITexasReportList,
	IVenueProfitLossDayList,
	IVenueProfitLossStateDay,
	IVenueProfitLossStateVenue,
	IVenueProfitLossStateVenueList,
	IVenueState,
	VpiueryProxyReportList
} from "./types"

const queryList = (uri: string) => (params: IQueryAgentProfitLoss & PageType) => http.post<IAgentProfitLossList>(uri, params)

const querySummary = (uri: string) => (params: IQueryAgentProfitLoss) => http.post<IAgentProfitLossState>(uri, params)

const queryDetail = (uri: string) => (params: IQueryAgentProfitLossDetail & PageType) =>
	http.post<IAgentProfitLossDetailList>(uri, params)

export const queryAgentProfitLossListByDay = queryList("/report/proxyNetAmount/day/listPage")
export const queryAgentProfitLossListByDayTotal = (params: IQueryAgentProfitLoss & PageType) =>
	http.post<IAgentProfitDayTotalList>("/report/proxyNetAmount/recalculate/day/total/listPage", params)
export const queryAgentProfitLossListByMonth = queryList("/report/proxyNetAmount/month/listPage")
export const queryAgentProfitLossSummaryByDay = querySummary("/report/proxyNetAmount/recalculate/day/summary")
export const queryAgentProfitLossSummaryByMonth = querySummary("report/proxyNetAmount/month/summary")
export const queryAgentProfitLossDetailByDay = queryDetail("report/proxyNetAmount/day/detail")
export const queryAgentProfitLossDetailByMonth = queryDetail("report/proxyNetAmount/day/detail")

export const queryGameList = () => http.get<IGameList>("/game/getVenueGames")

export const queryGameProfitList = (params: IQueryGameProfitQuery & PageType) =>
	http.post<IGameProfitList>("/report/gameProfit/listPage", params)
export const queryGameProfitSummary = (params: IQueryGameProfitQuery) =>
	http.post<IGameProfitSummaryState>("/report/gameProfit/summary", params)
export const queryGameProfitDetail = (params: IQueryGameProfitDetail & PageType) =>
	http.post<IGameProfitList>("/report/gameProfit/detailListPage", params)

export const exportAgentProfitLossDay = (params: IQueryAgentProfitLoss) =>
	http.post<any>("/report/proxyNetAmount/day/export", params)

export const exportAgentProfitLossMonth = (params: IQueryAgentProfitLoss) =>
	http.post<any>("/report/proxyNetAmount/month/export", params)

export const exportGameProfitLoss = (params: IQueryGameProfitQuery) => http.post<any>("/report/gameProfit/export", params)

const useApi = () => {
	const [loading, setLoading] = useState(false)

	async function generalHttp<T>(url: string, body: object | undefined) {
		setLoading(true)
		const response = await http.post<T>(url, body)
		setLoading(false)
		return response
	}

	const getMemberProfitAndLoss = (params: IProxyProfitAndLossListParams & PageType) =>
		generalHttp<Business.TableResourceType<IProxyProfitAndLoss>>("/report/membernetamount/member/listPage", params)

	const getMemberProfitAndLossSummary = (params: IProxyProfitAndLossListParams & PageType) =>
		generalHttp<IProxyProfitAndLoss>("/report/membernetamount/member/summary", params)

	const getDailyProfitAndLoss = (params: IProxyProfitAndLossListParams & PageType) =>
		generalHttp<Business.TableResourceType<IProxyProfitAndLoss>>("/report/daynetamount/list", params)

	const getDailyProfitAndLossSummary = (params: IProxyProfitAndLossListParams & PageType) =>
		generalHttp<IProxyProfitAndLoss>("/report/daynetamount/aggregation", params)

	const getMemberNetAmountDetail = (params: IGetMemberNetAmountDetailParams) =>
		generalHttp<IGetMemberNetAmountDetailResponse>("/report/membernetamount/list/venue/details/new", params)

	const getMemberNetAmountSummary = (params: IGetMemberNetAmountDetailParams) =>
		generalHttp<IGetMemberNetAmountDetailResponse>("/report/membernetamount/list/venue/summary/new", params)

	const queryDataCompareDayList = (params: IDataCompareDayParams & PageType) =>
		generalHttp<Business.TableResourceType<IDataCompareDay>>("/report/venueNetAmountDay/dataCompareDayList", params)

	const queryDataCompareDaySummary = (params: IDataCompareDayParams) =>
		generalHttp<IDataCompareDay>("/report/venueNetAmountDay/dataCompareDaySummary", params)

	const queryDailyDxReportD = (params: IDailyDxReportDParams & PageType) =>
		generalHttp<IDailyDxReportD[]>("/report/daily/dxReportD", params)

	const queryDetailDailyDxReportD = (params: IDailyDetailDxReportDParams) =>
		generalHttp<IDailyDetailData[]>("/report/daily/financialData", params)

	return {
		generalHttp,
		loading,
		getMemberProfitAndLoss,
		getMemberProfitAndLossSummary,
		getMemberNetAmountDetail,
		getMemberNetAmountSummary,
		queryDataCompareDayList,
		queryDataCompareDaySummary,
		getDailyProfitAndLoss,
		getDailyProfitAndLossSummary,
		queryDailyDxReportD,
		queryDetailDailyDxReportD
	}
}

export default useApi

const queryVenueDayList = (uri: string) => (params: IQueryVenueProfitLoss & PageType) =>
	http.post<IVenueProfitLossDayList>(uri, params)

export const queryVenueByDayList = queryVenueDayList("/report/venueNetAmountDay/listPage/new")
export const queryVenueByDaySummary = (params: IQueryVenueProfitLoss) =>
	http.post<IVenueProfitLossStateDay>("/report/venueNetAmountDay/summary/new", params)
export const queryVenueList = (params: IQueryVenueProfitLoss & PageType) =>
	http.post<IVenueProfitLossStateVenueList>("/report/venueNetAmountDay/listPage", params)
export const queryVenueSummary = (params: IQueryVenueProfitLoss) =>
	http.post<IVenueProfitLossStateVenue>("/report/venueNetAmountDay/summary", params)
export const queryVenueProfitDetail = (params: IQueryVenueProfitDetail & PageType) =>
	http.post<IVenueProfitLossStateVenueList>("/report/venueNetAmountDay/detailListPage", params)
export const exportVenueByDay = (params: IQueryVenueProfitLoss) => http.post<any>("/report/venueNetAmountDay/export", params)
export const exportVenueNew = (params: IQueryVenueProfitLoss) => http.post<any>("/report/venueNetAmountDay/export/new", params)

export const queryVenue = () => http.post<IVenueState[]>("/report/venueNetAmountDay/queryVenueNameList", {})

export const queryStatusOverviewTripartiteList = (params: IQueryStatusOverviewTripartite & PageType) =>
	http.post<IStatusOverviewTripartiteList>("/report/platform/third/list", params)

export const queryStatusOverviewTripartiteSummary = (params: IQueryStatusOverviewTripartite) =>
	http.post<IStatusOverviewTripartiteState>("/report/platform/third/summary", params)

export const queryStatusOverviewTripartiteBottom = (params: IQueryStatusOverviewTripartite & PageType) =>
	http.post<IStatusBottomTripartiteList>("/platformBottomRecord/page", params)
export const addStatusOverviewTripartiteBottom = (params: IBottomQuery) => http.post<any>("/platformBottomRecord/add", params)
export const updateStatusOverviewTripartiteBottom = (params: IBottomQuery) =>
	http.post<any>("/platformBottomRecord/update", params)
export const deleteStatusOverviewTripartiteBottom = (params: IBottomQuery) =>
	http.post<any>("/platformBottomRecord/delete", params)

export const getOutline = (userName: string, merchantId: string) =>
	http.get<any>(`/member/outlineInfo?userName=${userName}&merchantId=${merchantId}`)

export const exportStatusOverviewTripartite = (params: IQueryStatusOverviewTripartite) =>
	http.post<any>("/report/platform/third/export", params)

export const queryMemberLossRankingList = (params: IQueryMemberLossRanking & PageType) =>
	http.post<IMemberLossRankingList>("/report/dataReport/queryMembersProfitAndLossTop", params)

export const queryMemberLossRankingTotal = (params: IQueryMemberLossRanking) =>
	http.post<IMemberLossRankingTotal>("/report/dataReport/queryMembersProfitAndLossTopTotal", params)

export const exportMemberLossRankingTotal = (params: IQueryMemberLossRanking) =>
	http.post<any>("/report/dataReport/membersProfitAndLossTopExport", params)

export const queryRebateReconciliationList = (params: IQueryRebateReconciliation & PageType) =>
	http.post<IRebateReconciliationList>("/report/memberRebateAccount/list", params)

export const exportRebateReconciliationList = (params: IQueryRebateReconciliation) =>
	http.post<any>("/report/memberRebateAccount/export", params)

export const queryRebateVenues = () => http.post<IRebateVenueState[]>("/venue/getVenues", {})

export const queryMemberExpectedList = (params: IQueryMemberExpected & PageType) =>
	http.post<IMemberExpectedList>("/report/commission/member/expectRebatePage", params)

export const exportMemberExpectedList = (params: IQueryMemberExpected) =>
	http.post<any>("/report/commission/member/expectRebateExport", params)

export const queryMemberRateDetailList = (params: IQueryMemberRateDetail) =>
	http.post<IMemberRateDetailList>("/memberRebate/getMemberRateDetailPage", params)

export const queryMemberTeamRebateList = (params: IQueryMemberTeamRebate) =>
	http.post<IMemberTeamRebate[]>("/memberRebate/getMemberTeamRebateV2", params)

export const queryBetDetailList = (params: IQueryBetDetail & PageType) =>
	http.post<IBetDetailList>("/report/venueNetAmountDay/betDetail/list", params)

export const queryMemberReturnDataList = (params: IMemberReturnDataQueryParams & PageType) =>
	http.post<IMemberReturnDataList>("/report/memberRebateData/list", params)

export const queryMemberReturnDataSummary = (params: IMemberReturnDataQueryParams & PageType) =>
	http.post<IMemberReturnDataSummary>("/report/memberRebateData/summary", params)

export const queryMemberReturnDataDialog = (params: IMemberReturnDataReportParams) =>
	http.post<IMemberReturnDataReportList>("/report/memberRebateData/personRebate", params)

export const getAgentAndLossRakingDataList = (params: IAgentAndLossRankingQueryParams & PageType) =>
	http.post<IAgentAndLossRakingDataList>("/report/dataReport/queryAgencyTeamTop", params)

export const getAgentAndLossRakingDataSummary = (params: IAgentAndLossRankingQueryParams & PageType) =>
	http.post<IAgentAndLossRakingDataList>("/report/dataReport/queryAgencyTeamTopTotal", params)

export const queryNameList = (params: any) => http.post<IName[]>("/proxy/level/nameList", params)

export const queryProxyNetAmountRecal = (params: IProxyNetAmountQuery & PageType) =>
	http.post<IProxyNetAmountRecalList>("/report/proxyNetAmount/recalculate/day/listPage", params)

export const queryProxyNetAmountMemberOrderList = (params: IQueryProxyNetAmountOrder & PageType) =>
	http.post<IProxyNetAmountOrderList>("/reportRecal/proxyNetAmountMemberOrderList", params)

export const queryProxyTeamRebateAmountMemberOrderList = (params: IQueryProxyNetAmountOrder & PageType) =>
	http.post<IProxyNetAmountOrderList>("/reportRecal/proxyTeamRebateAmountMemberOrderList", params)

export const queryTexasReport = (params: IQueryStatusOverviewTripartite & PageType) =>
	http.post<ITexasReportList>("/report/platformDz/listPage", params)

export const queryProxyReport = (params: IQueryProxyReportList & PageType) =>
	http.post<IProxyReportList>("/report/ProxyReport/listPage", params)

export const queryMemberReport = (params: IQueryMemberReport & PageType) =>
	http.post<IMemberReportList>("/report/playerStaticInfo/listPage", params)

export const queryMemberReportDetail = (params: IMemberReportQueryParams & PageType) =>
	http.post<IPlayerDailyStatsList>("/report/playerReport/detailListPage", params)

export const queryMemberBalance = (params: IMemberReportQueryParams) => http.post<BalanceInfo>("/member/detail/balance", params)
export const queryProxyReportVip = (params: IQueryProxyReportList & PageType) =>
	http.post<IProxyReportList>("/report/playerStaticInfo/listPage", params)

export const querybalanceVip = (params: VpiueryProxyReportList & PageType) =>
	http.post<IProxyReportList>("/member/detail/balance", params)
